﻿#if UNITY_ANDROID
using System.Collections.Generic;
using GMME.Model.Rtm.Notify;
using GMME.Model.Rtm.Result;
using UnityEngine;

namespace GMME
{
    public class CallbackHandlerForAndroid : CallbackHandlerForEditor
    {
        public void onMuteAllPlayers(string roomId, AndroidJavaObject openIds, bool isMuted, int code, string msg)
        {
            List<string> openIdList = openIds == null ? new List<string>() : ConvertJavaList(openIds);
            string openIdListStr = string.Join(",", openIdList);
            Debug.LogFormat("handler onMutePlayer.roomId ={0}, isMuted={1}, code={2}, msg={3}",
                roomId, isMuted, code, msg);
            onMuteAllPlayers(roomId, openIdList, isMuted, code, msg);
        }

        public void onSpeakersDetection(AndroidJavaObject openIds)
        {
        }

        public void onSpeakersDetectionEx(AndroidJavaObject userVolumeInfos)
        {
            List<VolumeInfo> list = new List<VolumeInfo>();
            if (userVolumeInfos == null)
            {
                onSpeakersDetectionExComplete(list);
                return;
            }

            AndroidJavaObject[] userVolumeInfoArray = userVolumeInfos.Call<AndroidJavaObject[]>("toArray");
            List<string> openIdList = new List<string>(list.Count);
            foreach (AndroidJavaObject userVolumeInfo in userVolumeInfoArray)
            {
                VolumeInfo volumeInfo = new VolumeInfo();
                volumeInfo.OpenId = userVolumeInfo.Call<string>("getOpenId");
                volumeInfo.Volume = userVolumeInfo.Call<int>("getVolume");
                list.Add(volumeInfo);
                openIdList.Add(volumeInfo.OpenId);
            }

            onSpeakersDetectionEx(list, openIdList);
        }

        public void onForbidAllPlayers(string roomId, AndroidJavaObject openIds, bool isForbidden, int code, string msg)
        {
            List<string> openIdList = openIds == null ? new List<string>() : ConvertJavaList(openIds);
            string openIdListStr = string.Join(",", openIdList);
            Debug.LogFormat("handler onForbidAllPlayers.roomId ={0}, isForbidden={1}, code={2}, msg={3} ",
                roomId, isForbidden, code, msg);
            onForbidAllPlayers(roomId, openIdList, isForbidden, code, msg);
        }

        public void onForbiddenByOwner(string roomId, AndroidJavaObject openIds, bool isForbidden)
        {
            List<string> openIdList = openIds == null ? new List<string>() : ConvertJavaList(openIds);
            Debug.LogFormat("handler onForbiddenByOwner.roomId ={0}, openIds size ={1}, isForbidden={2} ", roomId,
                openIdList.Count, isForbidden);
            onForbiddenByOwnerComplete(roomId, openIdList, isForbidden);
        }


        private List<string> ConvertJavaList(AndroidJavaObject javaObject)
        {
            List<string> cSharpList = new List<string>();
            if (javaObject != null)
            {
                string[] openIdArray = javaObject.Call<string[]>("toArray");
                foreach (string openId in openIdArray)
                {
                    cSharpList.Add(openId);
                }
            }

            return cSharpList;
        }

        public void onAudioClipStateChangedNotify(AndroidJavaObject audioPlayStateInfoJavaObject)
        {
            Debug.LogFormat("handler onAudioClipStateChangedNotify. the value is : {0}",
                audioPlayStateInfoJavaObject.ToString());

            AudioPlayStateInfo audioPlayStateInfo =
                AudioPlayStateInfo.ConvertAudioPlayStateInfo(audioPlayStateInfoJavaObject);
            onAudioClipStateChangedNotify(audioPlayStateInfo);
        }

        #region RTM

        /**
         * 订阅RTM频道回调
         *
         * @param result 订阅RTM频道结果
         */
        public void onSubscribeRtmChannel(AndroidJavaObject result)
        {
            SubscribeRtmChannelResult subscribeRtmChannelResult =
                SubscribeRtmChannelResult.ConvertSubscribeRtmChannelResult(result);
            onSubscribeRtmChannel(subscribeRtmChannelResult);
        }

        /**
         * 取消订阅RTM频道回调
         *
         * @param result 取消订阅RTM频道结果
         */
        public void onUnSubscribeRtmChannel(AndroidJavaObject result)
        {
            UnSubscribeRtmChannelResult unSubscribeRtmChannelResult =
                UnSubscribeRtmChannelResult.ConvertUnSubscribeRtmChannelResult(result);
            onUnSubscribeRtmChannel(unSubscribeRtmChannelResult);
        }

        /**
         * 发布RTM频道消息回调
         *
         * @param result 发布RTM频道消息结果
         */
        public void onPublishRtmChannelMessage(AndroidJavaObject result)
        {
            PublishRtmChannelMessageResult publishRtmChannelMessageResult =
                PublishRtmChannelMessageResult.ConvertPublishRtmChannelMessageResult(result);
            onPublishRtmChannelMessage(publishRtmChannelMessageResult);
        }

        /**
         * 发布RTM点对点消息回调
         *
         * @param result 发布RTM点对点消息结果
         */
        public void onPublishRtmPeerMessage(AndroidJavaObject result)
        {
            PublishRtmPeerMessageResult peerMessageResult =
                PublishRtmPeerMessageResult.ConvertPublishRtmPeerMessageResult(result);
            onPublishRtmPeerMessage(peerMessageResult);
        }

        /**
         * 获取RTM频道信息回调
         *
         * @param result 获取RTM频道信息结果
         */
        public void onGetRtmChannelInfo(AndroidJavaObject result)
        {
            GetRtmChannelInfoResult getRtmChannelInfoResult =
                GetRtmChannelInfoResult.ConvertGetRtmChannelInfoResult(result);
            onGetRtmChannelInfo(getRtmChannelInfoResult);
        }

        /**
         * 接收RTM频道信息通知
         *
         * @param notify 接收RTM频道信息结果
         */
        public void onReceiveRtmChannelMessage(AndroidJavaObject notify)
        {
            ReceiveRtmChannelMessageNotify rtmChannelMessageNotify =
                ReceiveRtmChannelMessageNotify.ConvertReceiveRtmChannelMessageNotify(notify);
            onReceiveRtmChannelMessage(rtmChannelMessageNotify);
        }

        /**
         * 接收RTM点对点信息通知
         *
         * @param notify 接收RTM点对点信息结果
         */
        public void onReceiveRtmPeerMessage(AndroidJavaObject notify)
        {
            ReceiveRtmPeerMessageNotify receiveRtmPeerMessageNotify =
                ReceiveRtmPeerMessageNotify.ConvertReceiveRtmPeerMessageNotify(notify);
            onReceiveRtmPeerMessage(receiveRtmPeerMessageNotify);
        }

        /**
         * RTM连接状态通知
         *
         * @param notify RTM连接状态结果
         */
        public void onRtmConnectionChanged(AndroidJavaObject notify)
        {
            // 可根据需求将数据进行处理
            Debug.LogFormat("handler onRtmConnectionChanged. the value is : {0}", notify.ToString());

            RtmConnectionStatusNotify rtmConnectionStatusNotify =
                RtmConnectionStatusNotify.ConvertRtmConnectionStatusNotify(notify);
            onRtmConnectionChanged(rtmConnectionStatusNotify);
        }

        /**
         * 设置频道内玩家属性结果回调
         *
         * @param result 设置频道内玩家属性结果
         */
        public void onSetRtmChannelPlayerProperties(AndroidJavaObject result)
        {
            SetRtmChannelPlayerPropertiesResult setRtmChannelPlayerPropertiesResult =
                SetRtmChannelPlayerPropertiesResult.ConvertSetRtmChannelPlayerPropertiesResult(result);

            onSetRtmChannelPlayerProperties(setRtmChannelPlayerPropertiesResult);
        }

        /**
         * 查询频道内玩家属性结果回调
         *
         * @param result 查询频道内玩家属性结果
         */
        public void onGetRtmChannelPlayerProperties(AndroidJavaObject result)
        {
            GetRtmChannelPlayerPropertiesResult getRtmChannelPlayerPropertiesResult =
                GetRtmChannelPlayerPropertiesResult.ConvertGetRtmChannelPlayerPropertiesResult(result);
            onGetRtmChannelPlayerProperties(getRtmChannelPlayerPropertiesResult);
        }

        /**
         * 删除频道内玩家属性结果回调
         *
         * @param result 删除频道内玩家属性结果
         */
        public void onDeleteRtmChannelPlayerProperties(AndroidJavaObject result)
        {
            DeleteRtmChannelPlayerPropertiesResult deleteRtmChannelPlayerPropertiesResult =
                DeleteRtmChannelPlayerPropertiesResult.ConvertDeleteRtmChannelPlayerPropertiesResult(result);
            onDeleteRtmChannelPlayerProperties(deleteRtmChannelPlayerPropertiesResult);
        }

        /**
         * 设置频道属性结果回调
         *
         * @param result 设置频道属性结果
         */
        public void onSetRtmChannelProperties(AndroidJavaObject result)
        {
            SetRtmChannelPropertiesResult setRtmChannelPropertiesResult =
                SetRtmChannelPropertiesResult.ConvertSetRtmChannelPropertiesResult(result);
            onSetRtmChannelProperties(setRtmChannelPropertiesResult);
        }

        /**
         * 查询频道属性结果回调
         *
         * @param result 查询频道属性结果
         */
        public void onGetRtmChannelProperties(AndroidJavaObject result)
        {
            GetRtmChannelPropertiesResult getRtmChannelPropertiesResult =
                GetRtmChannelPropertiesResult.ConvertGetRtmChannelPropertiesResult(result);
            onGetRtmChannelProperties(getRtmChannelPropertiesResult);
        }

        /**
         * 删除频道属性结果回调
         *
         * @param result 删除频道属性结果
         */
        public void onDeleteRtmChannelProperties(AndroidJavaObject result)
        {
            DeleteRtmChannelPropertiesResult deleteRtmChannelPropertiesResult =
                DeleteRtmChannelPropertiesResult.ConvertDeleteRtmChannelPropertiesResult(result);
            onDeleteRtmChannelProperties(deleteRtmChannelPropertiesResult);
        }

        /**
         * 查询频道历史消息回调
         *
         * @param result 查询频道历史消息结果
         */
        public void onGetRtmChannelHistoryMessages(AndroidJavaObject result)
        {
            GetRtmChannelHistoryMessagesResult getRtmChannelHistoryMessagesResult =
                GetRtmChannelHistoryMessagesResult.ConvertGetRtmChannelHistoryMessagesResult(result);
            onGetRtmChannelHistoryMessages(getRtmChannelHistoryMessagesResult);
        }

        /**
         * 频道内玩家属性变更通知
         *
         * @param notify 频道内玩家属性变更
         */
        public void onRtmChannelPlayerPropertiesChanged(AndroidJavaObject notify)
        {
            RtmChannelPlayerPropertiesNotify rtmChannelPlayerPropertiesNotify =
                RtmChannelPlayerPropertiesNotify.ConvertRtmChannelPlayerPropertiesNotify(notify);
            onRtmChannelPlayerPropertiesChanged(rtmChannelPlayerPropertiesNotify);
        }

        /**
         * 频道属性变更通知
         *
         * @param notify 频道属性变更
         */
        public void onRtmChannelPropertiesChanged(AndroidJavaObject notify)
        {
            RtmChannelPropertiesNotify rtmChannelPropertiesNotify =
                RtmChannelPropertiesNotify.ConvertRtmChannelPropertiesNotify(notify);
            onRtmChannelPropertiesChanged(rtmChannelPropertiesNotify);
        }

        #endregion
    }
}
#endif